package com.winderinfo.yikaotianxia.ui.zixun;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaselanFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class InformationFragment extends BaselanFragment implements Contract.View {


    @BindView(R.id.b_recycle)
    RecyclerView bRecycle;
    Unbinder unbinder;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected int layoutResID() {
        return R.layout.activity_banxin;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
        //适配管理器
        bRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));

    }

    @Override
    protected void loadData() {
        //资讯分类ID
        int position = getArguments().getInt("position");
        //请求资讯列表数据
        /**
         * @tuijian 0不推荐 1推荐
         * @zixuntype 资讯分类Id
         *
         * 当资讯分类id为1展示推荐的数据，其它不传@tuijian
         * */
        map.clear();
        if (position == 1) {
            map.clear();
            map.put("tuijian", 1);
        } else {
            map.clear();
            map.put("zixuntype", position);
        }
        presenter.onPostRequest(Api.ZIXUNLIEBIAO, map, InformationListBean.class);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onSuccess(Object data) {
        if (data instanceof InformationListBean) {
            List<InformationListBean.RowsBean> rows = ((InformationListBean) data).getRows();
            InformationListAdapter informationListAdapter = new InformationListAdapter(getActivity(), rows);
            bRecycle.setAdapter(informationListAdapter);
            //接口回调
            informationListAdapter.setOnzhixunId(new InformationListAdapter.OnzhixunId() {
                @Override
                public void getId(int id) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("zxId", id);
                    openActivity(InformationXiangqin.class, bundle);
                }
            });
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
